package com.neusoft.di.controller;

import com.neusoft.di.entity.Person;
import com.neusoft.di.entity.common.CommonResult;
import com.neusoft.di.entity.common.ResultCode;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

@RestController
@RequestMapping("rv")
public class ReturnValuesController {
    @ExceptionHandler
    public Mono<String> handle(Exception ex) {
        // ...
        return Mono.just("OK");
    }


    @GetMapping("/success/e")
    public Mono<CommonResult<Person>> getException() {
        Mono<Person> person = Mono.just(new Person(1, "neusoft"));

        int i = 1 / 0;

        return person.flatMap(p -> {
            System.out.println("ReturnValuesController.getSuccessPerson" + p.toString());
            return Mono.just(CommonResult.success(p));
        });
    }


    @GetMapping("/success/p")
    public Mono<CommonResult<Person>> getSuccessPerson() {
        Mono<Person> person = Mono.just(new Person(1, "neusoft"));
        return person.flatMap(p -> {
            System.out.println("ReturnValuesController.getSuccessPerson" + p.toString());
            return Mono.just(CommonResult.success(p));
        });
    }


    @GetMapping("/success")
    public Mono<CommonResult<String>> getSuccess() {
        return Mono.just(CommonResult.success("This is a success message"));
    }

    @GetMapping("/failed")
    public Mono<CommonResult<String>> getFailed() {
        return Mono.just(CommonResult.failed(ResultCode.VALIDATE_FAILED));
    }


    @RequestMapping("t4")
    public Mono<Person> testMonoPerson() {


        return Mono.just(new Person(1, "neusoft"));
    }


    @RequestMapping("t3")
    public Mono<String> testMonoString() {
        return Mono.just("hello world");
    }


    @RequestMapping("t2")
    public Mono<Void> testMono() {

        return Mono.empty();
    }


    @RequestMapping("t1")
    public String testString() {

        return "hello world";
    }

    public Flux aaa() {
        return null;
    }
}
